package org.hospital.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hospital.entity.Employee;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

public class LoginInterceptor implements HandlerInterceptor{
	
	 /** 
	 * Handler执行完成之后调用这个方法 
	 */  
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2,
			Exception arg3) throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	/** 
     * Handler执行之后，ModelAndView返回之前调用这个方法 
     */  
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2,
			ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	/** 
     * Handler执行之前调用这个方法 
     */ 
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2)
			throws Exception {
		
		System.out.println("LoginInterceptor.preHandle");
		HttpSession session = request.getSession();
		Employee loginUser = (Employee)session.getAttribute("employee");
		if(loginUser == null) {
			System.out.println("LoginInterceptor.preHandle : null");
			//request.getRequestDispatcher("/Hospital/login.html").forward(request, response);
			response.sendRedirect("/Hospital/login.html");
			return true;
		}
		System.out.println("LoginInterceptor.preHandle : " + JSON.toJSONString(loginUser));
		
		return true;
	}
	
}
